package com.xfmax.textcomponent.animator;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.LinearLayout;

import com.xfmax.textcomponent.animator.BaseViewAnimator;
import com.xfmax.textcomponent.animator.SlideInLeftAnimator;
import com.xfmax.textcomponent.animator.SlideInRightAnimator;
import com.xfmax.textcomponent.animator.SlideOutLeftAnimator;
import com.xfmax.textcomponent.animator.SlideOutRightAnimator;

public class AnimTools {
    public static final int SLIDE_OUTLEFT_INRIGHT = 0;//左出右进
    public static final int SLIDE_OUTRIGHT_INLEFT = 1;//右出左进

    public static void anim(final View view, int startHeight, int endHeight, int duration){
        ObjectAnimator anim = ObjectAnimator.ofInt(view, "view", startHeight,endHeight ).setDuration(duration);
        anim.start();
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int cVal = (int) valueAnimator.getAnimatedValue();
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
                lp.height = cVal;
                view.setLayoutParams(lp);
            }
        });
    }

    public static BaseViewAnimator play(int slideType, View target) {
        BaseViewAnimator slideOutAnimator = null;
        BaseViewAnimator slideInAnimator = null;
        if (slideType == SLIDE_OUTLEFT_INRIGHT) {
            slideOutAnimator = new SlideOutLeftAnimator();
            slideInAnimator = new SlideInRightAnimator();
        } else if (slideType == SLIDE_OUTRIGHT_INLEFT) {
            slideOutAnimator = new SlideOutRightAnimator();
            slideInAnimator = new SlideInLeftAnimator();
        } else {
            throw new RuntimeException("slide type is not valid");
        }
        slideOutAnimator.setTarget(target);
        slideInAnimator.setTarget(target);

        ViewCompat.setPivotX(target, target.getMeasuredWidth() / 2.0f);
        ViewCompat.setPivotY(target, target.getMeasuredHeight() / 2.0f);
//        if (pivotX == YoYo.CENTER_PIVOT) {
//            ViewCompat.setPivotX(target, target.getMeasuredWidth() / 2.0f);
//        } else {
//            target.setPivotX(pivotX);
//        }
//        if (pivotY == YoYo.CENTER_PIVOT) {
//            ViewCompat.setPivotY(target, target.getMeasuredHeight() / 2.0f);
//        } else {
//            target.setPivotY(pivotY);
//        }

        slideOutAnimator.setDuration(500)
                .setRepeatTimes(0)
                .setRepeatMode(ValueAnimator.RESTART)
                .setInterpolator(new AccelerateDecelerateInterpolator())
                .setStartDelay(0);

//        if (callbacks.size() > 0) {
//            for (Animator.AnimatorListener callback : callbacks) {
//                animator.addAnimatorListener(callback);
//            }
//        }

        slideInAnimator.setDuration(500)
                .setRepeatTimes(0)
                .setRepeatMode(ValueAnimator.RESTART)
                .setInterpolator(new AccelerateDecelerateInterpolator())
                .setStartDelay(300);

//        if (callbacks.size() > 0) {
//            for (Animator.AnimatorListener callback : callbacks) {
//                animator.addAnimatorListener(callback);
//            }
//        }
        slideOutAnimator.animate();
        slideInAnimator.animate();

        return slideInAnimator;
    }



}
